package kd.cosmic.formplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.EventObject;

public class addMoney extends AbstractFormPlugin {
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("btnok");
    }

    @Override
    public void beforeClick(BeforeClickEvent evt) {
        super.beforeClick(evt);

        Object xhzhIdnumber = this.getModel().getValue("xhzh_idnumber");
        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("xhzh_school_card", "xhzh_money",
                new QFilter[]{new QFilter("xhzh_idnumber", QCP.equals, xhzhIdnumber)});

        if(dynamicObject != null){
            return;
        }

        this.getView().showMessage("抱歉，请确保您是否注册校园卡，无法查询!");
        evt.setCancel(true);
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);

        Object xhzhIdnumber = this.getModel().getValue("xhzh_idnumber");
        DynamicObject[] dynamicObject = BusinessDataServiceHelper.load("xhzh_school_card", "xhzh_money",
                new QFilter[]{new QFilter("xhzh_idnumber", QCP.equals, xhzhIdnumber)});
        Object xhzhAddMoney = this.getModel().getValue("xhzh_add_money_");
        Integer addmoney = Integer.valueOf((String) xhzhAddMoney);
        Integer alreadyHave = Integer.valueOf((String) dynamicObject[0].get("xhzh_money"));
        dynamicObject[0].set("xhzh_money",String.valueOf(addmoney+alreadyHave));

        SaveServiceHelper.save(dynamicObject);

    }
}
